﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Data.OleDb;
using System.Configuration;

/// <summary>
/// Summary description for Zalog_servis
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
//
public class Zalog_servis : System.Web.Services.WebService {

    public Zalog_servis () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }
    
    
   

    [WebMethod]
    public string HelloWorld() {
        return "Hello World";
    }
    
    
    //Proekt_Banka proekt5 = new Proekt_Banka();// object od proekt05
    [WebMethod]//za da mozeme da ja stajme pod hipoteka brojot na kuki i hoteli na imotot mora da e 0
    public int getBrKuki( String grad)
    {
        String conString = ConfigurationManager.ConnectionStrings["konekcija"].ConnectionString;
        OleDbConnection conn = new OleDbConnection(conString);

        conn.Open();
        String select = "SELECT Kuki FROM Gradovi WHERE  Grad=? ";
        OleDbCommand cmd = new OleDbCommand(select, conn);
        
        cmd.Parameters.AddWithValue("?", grad);
        int brKuki = Convert.ToInt32(cmd.ExecuteScalar());
        conn.Close();
        return brKuki;
    }
    [WebMethod]
    public int getBrHoteli(String grad)
    {
        String conString = ConfigurationManager.ConnectionStrings["konekcija"].ConnectionString;
        OleDbConnection conn = new OleDbConnection(conString);

        conn.Open();
        String select = "SELECT Hoteli FROM Gradovi WHERE Grad=? ";
        OleDbCommand cmd = new OleDbCommand(select, conn);
        
        cmd.Parameters.AddWithValue("?", grad);
        int brHoteli = Convert.ToInt32(cmd.ExecuteScalar());
        conn.Close();
        return brHoteli;
    }
    [WebMethod]
    public Boolean imaHipoteka(String grad)//Proveruvame dali veke ima hipoteka za dadeniot grad
    {


        String conString = ConfigurationManager.ConnectionStrings["konekcija"].ConnectionString;
        OleDbConnection conn = new OleDbConnection(conString);

        conn.Open();
        String select = "SELECT Hipoteka FROM Gradovi WHERE Grad=?";
        OleDbCommand cmd = new OleDbCommand(select, conn);
        cmd.Parameters.AddWithValue("?", grad);
        int yes_no = Convert.ToInt32(cmd.ExecuteScalar()); 
        if (yes_no == 0)
        {
            conn.Close();
            return true;
        }
        else
        {
            conn.Close();
            return false;
        }
    }
    [WebMethod]
    public void StaviPodHipoteka(String grad, String ime)//metod za stavanje pod hipoteka na dadeniot grad
    {
        Boolean tocno=imaHipoteka(grad);
        if (!tocno && (this.getBrHoteli(grad)==0) && (this.getBrKuki(grad)==0) )
        {
            String conString = ConfigurationManager.ConnectionStrings["konekcija"].ConnectionString;
            OleDbConnection conn = new OleDbConnection(conString);

            conn.Open();
            String select = "SELECT CenaGrad FROM Gradovi WHERE Grad=? ";
            OleDbCommand cmd = new OleDbCommand(select, conn);
            cmd.Parameters.AddWithValue("?",grad);            
            int cena=Convert.ToInt32(cmd.ExecuteScalar());
            int dobiva = cena / 2;//podoleno e so 2 zatoa sto nema vo bazata vrednost na dadeniot imot
            //proekt5.depositMoney(ime,dobiva); go povikuvame metodot od proekt br.5 
            String updat = "UPDATE Igrac SET VkupnoPari=VkupnoPari+? WHERE Igrac=?";
            cmd = new OleDbCommand(updat, conn);
            cmd.Parameters.AddWithValue("?", dobiva);
            cmd.Parameters.AddWithValue("?",ime);
            cmd.ExecuteNonQuery();
            String hip = "UPDATE Gradovi SET Hipoteka=? WHERE Grad=?";
            cmd = new OleDbCommand(hip, conn);
            int a = 0;
            cmd.Parameters.AddWithValue("?",a);
            cmd.Parameters.AddWithValue("?", grad);
            cmd.ExecuteNonQuery();
            conn.Close();
            
        }
        
    }
    [WebMethod]
    public void PlatiHipoteka(String grad, String ime)
    {
        Boolean ima = imaHipoteka(grad);
        if (ima)
        {
            String conString = ConfigurationManager.ConnectionStrings["konekcija"].ConnectionString;
            OleDbConnection conn = new OleDbConnection(conString);

            conn.Open();

            String pari = "SELECT CenaGrad FROM Gradovi WHERE Grad=?";
            OleDbCommand cmd = new OleDbCommand(pari, conn);
            cmd.Parameters.AddWithValue("?", grad)  ;
            
            int cena = Convert.ToInt32(cmd.ExecuteScalar());
            int zaplakanje = cena / 2;
            //proekt5.withdrawMoney(ime,zaplakanje); od proekt 5
            String updat = "UPDATE Igrac SET VkupnoPari=VkupnoPari-? WHERE Igrac=?";
            cmd = new OleDbCommand(updat, conn);
            cmd.Parameters.AddWithValue("?", zaplakanje);
            cmd.Parameters.AddWithValue("?", ime);
            cmd.ExecuteNonQuery();


            String hip = "UPDATE Gradovi SET Hipoteka=? WHERE Grad=?";
            int a = -1;
            cmd = new OleDbCommand(hip, conn);
            cmd.Parameters.AddWithValue("?", a);
            cmd.Parameters.AddWithValue("?", grad);
            cmd.ExecuteNonQuery();
            conn.Close();


        }
    }
    
}
